
/*jslint bitwise: true, eqeqeq: true, immed: true, newcap: true, nomen: false,
 onevar: false, plusplus: false, regexp: true, undef: true, white: true, indent: 2
 browser: true */

/*global jQuery: true Drupal: true window: true */

(function ($) {
  Drupal.behaviors.rotatingBanner = {
    attach: function (context) {
      $('.rotating-banner', context).once('rotatingActivated', function () {
        if (!Drupal.settings.rotatingBanners) {
          return;
        }
				var slidetype = Drupal.settings.rotatingBanners[this.id].type;
				
				// Camera
				if (slidetype == "1") {
          var settings = Drupal.settings.rotatingBanners[this.id].camera;
					if (Drupal.settings.rotatingBanners[this.id].fluid == "1") {
					  settings.height = '42%';
					}
          if ($.fn.camera === 'undefined' || $.fn.camera === undefined) {
            alert(Drupal.t('Camera js is not installed and is required by the rotating_banner module.\n\nSee the README.txt'));
            return;
          }
					fx = '';
					$.each(settings.fx, function(key, value) { 
						fx += value + ',';
					});
					settings.fx = fx.slice(0, -1);
					if (settings.thumbnails == '1') {
						settings.thumbnails = true;
					} else {
						settings.thumbnails = false;
					}
					settings.loaderPadding = parseInt(settings.loaderPadding);
					settings.loaderStroke = parseInt(settings.loaderStroke);
					//delete settings.splice(piePosition, 1);
					if (settings.loaderColor == '') {
						settings.loaderColor = '#eeeeee'
					}
					if (settings.loaderBgColor == '') {
						settings.loaderBgColor = '#222222'
					}

          $('.rb-slides-camera', this).camera(settings);
				}
				// Nivo
				if (slidetype == "2") {
          var settings = Drupal.settings.rotatingBanners[this.id].nivo;
          if ($.fn.nivoSlider === 'undefined' || $.fn.nivoSlider === undefined) {
            alert(Drupal.t('Nivo Slider is not installed and is required by the rotating_banner module.\n\nSee the README.txt'));
            return;
          }
					fx = '';
					$.each(settings.fx, function(key, value) { 
						fx += value + ',';
					});
					settings.fx = fx.slice(0, -1);
					settings.slices = parseInt(settings.slices);
          $('.rb-slides-nivo', this).nivoSlider(settings);
				}
				// Cycle
				if (slidetype == "3") {
          var settings = Drupal.settings.rotatingBanners[this.id].cycle;
          if ($.fn.cycle === 'undefined' || $.fn.cycle === undefined) {
            alert(Drupal.t('Jquery Cycle is not installed and is required by the rotating_banner module.\n\nSee the README.txt'));
            return;
          }
					fx = '';
 					$.each(settings.fx, function(key, value) { 
						fx += value + ',';
					});
					settings.fx = fx.slice(0, -1);
          settings.fit = 1;
          settings.cleartypeNoBg = true;
          if(settings.controls == 'prev_next') {
            settings.prev = "#" + this.id + " .prev";
            settings.next = "#" + this.id + " .next";
          } else {
            settings.pager = "#" + this.id + " .controls";
          }

          $('.rb-slides-cycle', this).cycle(settings);
				}
				// Kwick
				if (slidetype == "4") {
					$(window).load(function() {
							$(".kwicks").find("img").fadeIn("fast");
					});
					if ($.fn.kwicks === 'undefined' || $.fn.kwicks === undefined) {
            alert(Drupal.t('Jquery Cycle is not installed and is required by the rotating_banner module.\n\nSee the README.txt'));
            return;
          }
					
					rows = $('li.kwick', this).length;
					itemwidth = Math.floor(Drupal.settings.rotatingBanners[this.id].width/rows);
					kwichsmax = Drupal.settings.rotatingBanners[this.id].width - itemwidth;
					
					$('.kwick_desc').css({
						'width': kwichsmax - 30
					});
					$('.kwick .kwick_title').css({
							'opacity': '0.7'
					});
					$('.kwick').hover(function() {
						$(this).find(".kwick_title").stop(true, true).fadeTo(500, 0);
					}, function() {
						$(this).find(".kwick_title").stop(true, true).fadeTo(500, 0.7);
					});
					$(".kwick").hover(function() {
						$(this).find(".kwick_desc").animate({
							'opacity': '1',
							bottom: "0px"
						}, {
							queue: false,
							duration: 500
						});
					}, function() {
						$(this).find(".kwick_desc").animate({
							'opacity': '0',
							bottom: "-300px"
						}, {
							queue: false,
							duration: 500
						});
					});
					
          var settings = Drupal.settings.rotatingBanners[this.id].kwicks;
          $('ul.kwicks', this).kwicks({
						easing: settings.easing,
						max : kwichsmax
					});
				}
				
      });
    }
  };
})(jQuery);
